<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="用户名" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" placeholder="密码" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" placeholder="邮箱" />
      </el-form-item>
      <el-form-item label="家庭住址">
        <el-input v-model="form.address" placeholder="家庭住址" />
      </el-form-item>
      <el-form-item label="传真">
        <el-input v-model="form.fax" placeholder="传真" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.mobile" placeholder="手机号" />
      </el-form-item>
      <el-form-item label="座机号">
        <el-input v-model="form.landlineNumber" placeholder="座机号" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="备注" />
      </el-form-item>
      <el-form-item label="所属部门">
        <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" />
      </el-form-item>

      <el-form-item label="角色">

        <el-checkbox v-for="r in rlists" :key="r.roleId" v-model="form.rids" :label="r.roleId">{{ r.roleName }}</el-checkbox>

      </el-form-item>
      <el-form-item label="请选择">
        <el-select v-model="form.shengji" placeholder="省" @change="changeprovince()">
          <el-option
            v-for="item in seng"
            :key="item.regionName"
            :label="item.regionName"
            :value="item.regionId"
          />
        </el-select>
        <el-select v-model="form.shiji" placeholder="市" @change="changeprovince1()">
          <el-option
            v-for="item in shi"
            :key="item.regionName"
            :label="item.regionName"
            :value="item.regionId"
          />
        </el-select>
        <el-select v-model="form.quji" placeholder="区" @change="changeprovince2()">
          <el-option
            v-for="item in qu"
            :key="item.regionName"
            :label="item.regionName"
            :value="item.regionId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商圈">

        <el-checkbox v-for="c in cityts" :key="c.id" v-model="form.cityc" :label="c.id">{{ c.name }}</el-checkbox>

      </el-form-item>
      <el-form-item label="状态">
        <div>
          <el-radio-group v-model="form.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="primary" @click="fanhui">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'Update',
  data() {
    return {
      data: [],
      form: {
        userId: null,
        username: null,
        password: null,
        email: null,
        address: null,
        fax: null,
        mobile: null,
        landlineNumber: null,
        remark: null,
        status: null,
        deptId: null,
        shengji: null,
        shiji: null,
        quji: null,
        rids: [],
        cityc: []
      },
      seng: [],
      shi: [],
      qu: [],
      cityts: [],
      rlists: [],
      defaultProps: {
        children: 'depts',
        label: 'name'
      }
    }
  },
  created() {
    this.huixian()
    this.sheng()
    this.handleChange()
  },
  methods: {

    getDept() {
      alert('sacasvsdv')
    },
    onSubmit() {
      this.axios.post('http://localhost:8091/user/updatess', this.form).then(res => {
        if (res) {
          alert('修改成功')
          this.$router.push({ name: 'User' })
        }
      })
    },
    fanhui() {
      this.$router.go(-1)
    },
    sheng() {
      this.axios.post('http://localhost:8091/region/sheng').then((res) => {
        this.seng = res.data
      })
    },
    changeprovince() {
      this.axios.post('http://localhost:8091/region/shi?country=' + this.form.shengji).then((res) => {
        this.shi = res.data
      })
    },
    changeprovince1(val) {
      this.axios.post('http://localhost:8091/region/qu?city=' + this.form.shiji).then((res) => {
        this.qu = res.data
      })
    },
    handleChange(value) {
      this.axios.get('http://localhost:8091/user/getDeptss').then(res => {
        this.data = res.data.obj
      })
    },
    handleNodeClick(data) {
      this.form.deptId = data.deptId
      this.axios.get('http://localhost:8091/user/getrole?id=' + this.form.deptId).then(res => {
        this.rlists = res.data.obj
      })
    },
    handleNodeClicks() {
      this.axios.get('http://localhost:8091/user/getrole?id=' + this.form.deptId).then(res => {
        this.rlists = res.data.obj
      })
    },
    changeprovince2() {
      this.axios.get('http://localhost:8091/user/getshangquan?province=' + this.form.quji).then(res => {
        console.log(JSON.stringify(res.data))
        this.cityts = res.data.obj
      })
    },
    huixian() {
      this.form.userId = this.$route.params.id
      this.axios.post('http://localhost:8091/user/toupdate?id=' + this.form.userId).then(res => {
        this.form.userId = res.data.userId
        this.form.username = res.data.username
        this.form.password = res.data.password
        this.form.email = res.data.email
        this.form.address = res.data.address
        this.form.fax = res.data.fax
        this.form.mobile = res.data.mobile
        this.form.landlineNumber = res.data.landlineNumber
        this.form.status = res.data.status
        this.form.deptId = res.data.deptId
        this.form.remark = res.data.remark
        this.form.shengji = res.data.province
        this.form.shiji = res.data.city
        this.form.quji = res.data.country
      })
    }

  }
}
</script>
